import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	CreateDateColumn,
	UpdateDateColumn,
	ManyToOne,
	OneToMany,
	JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { OrderItem } from "./order-item.entity";
import { DateTransformer } from "../../../common/transformers/date.transformer";
import { Expose } from "class-transformer";

@Entity("orders")
export class Order {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({ length: 50, unique: true })
	orderNo: string;

	@Column()
	totalPoints: number;

	@Column({
		type: "enum",
		enum: ["待付款", "待发货", "已发货", "已完成", "已取消"],
		default: "待付款",
	})
	status: string;

	@Column({ length: 50, nullable: true })
	receiver: string;

	@Column({ length: 20, nullable: true })
	phone: string;

	@Column({ type: "text", nullable: true })
	address: string;

	@Column({ type: "text", nullable: true })
	remark: string;

	@CreateDateColumn({
		name: "created_at",
		type: "datetime",
		transformer: new DateTransformer(),
		nullable: false,
	})
	createdAt: Date;

	@UpdateDateColumn({
		name: "updated_at",
		type: "datetime",
		transformer: new DateTransformer(),
		nullable: false,
	})
	updatedAt: Date;

	// 关系
	@ManyToOne(() => User, (user) => user.orders)
	@JoinColumn({ name: "user_id" })
	@Expose()
	user: User;

	@Column()
	userId: number;

	@OneToMany(() => OrderItem, (orderItem) => orderItem.order, { cascade: true })
	@Expose()
	items: OrderItem[];
}
